<?
ini_set("display_errors",1);
include($_SERVER['DOCUMENT_ROOT'].'/includes/functions.inc.php');
include($_SERVER['DOCUMENT_ROOT'].'/portals/config.php');
include($_SERVER['DOCUMENT_ROOT'].'/groups/database.php');
include($_SERVER['DOCUMENT_ROOT'].'/portals/filter.libs.php');

init_session();

include($_SERVER['DOCUMENT_ROOT'].'/portals/header.inc.php');



$id = intval(@$_GET['id']);



if ($id) {
	$where = "`readable` = 'Y'"; 
	if (!empty($_SESSION['user_id'])) {
		$where = "($where OR user_id = ".dbQuote($_SESSION['user_id']).")";
	}
	$current = getRow("SELECT * FROM portal WHERE portal_id = $id AND $where");

        if ($current['status_id'] != 1) {
                die("unable to view this Portal");
        }

	
	print "<h3>View Portal Filter Definition</h3>";
} else {
	die("huh?");
}



if ($id ) {
	print "<p>Viewing Portal: <b>".he($current['title'])."</b>. ";

	if (!empty($_SESSION['user_id'])) {
		print "<a href=\"portal.php?id=$id\">Return to listing</a></p>";
	}
}

$query = getQueryProfile($id);



?>


<h2>Only show images that match...

<?

        if ($current['user_id'] == $_SESSION['user_id']) {
                print "<sub><a href=\"filters.php?id=$id\">Edit these filters</a></sub>";
        }

print "</h2>";

$data = getAll("SELECT * FROM filter WHERE portal_id = $id AND status_id = 1 AND value != '' ORDER BY type+0,filter_id");
	
$keys = array_keys($profiles);
$last = '';
$stats = array();
if ($data) foreach ($data as $idx => $row) {
	
	#####
	#the type

	if ($row['type'] != $last && (count($data) > 1 || $row['type'] == 'not')) {
		if ($last)
			print "</blockquote>";
		print "<h3 style=background-color:silver>";
		switch($row['type']) {
			case 'any': print "ANY of these filters"; break;
			case 'all': print "ALL of these filters"; break;
			case 'not': print "NONE of these filters"; break;
		}
		print "</h3><blockquote>";
		$last = $row['type'];
	}
	$filter_id = $row['filter_id'];
	
	@$stats[$row['type']][$row['profile']]++;
	
	######
	# the title
	
	if (!empty($row['title'])) {
		print "<h4>".($idx+1).". ".he($row['title'])." <sub style=color:gray>({$profiles[$row['profile']]})</sub>";
	} else {
		print "<h4>".($idx+1).". {$profiles[$row['profile']]}";
	}
	if ($row['profile'] == 'sphinx') {
		print "  <sup><a href=\"http://www.geograph.org.uk/article/Word-Searching-on-Geograph\" target=\"_blank\" title=\"Supports the full 'Word Searching on Geograph' syntax\">?</a></sup>";
	}
	print "</h4>";
	
	######
	# show the rule
	
	if ($row['profile'] == 'sphinx') {
		print "<ul><li><tt><a href=\"http://www.geograph.org.uk/search.php?searchtext=".urlencode($row['value'])."&amp;do=1\">".he($row['value'])."</a></tt></li></ul>";
	} elseif ($row['profile'] == 'words') {
		print "<ul><li><tt>".trim(preg_replace('/[^\w]+/',' ',$row['value']))."</tt></li></ul>";
	} elseif ($row['profile'] == 'phrase') {
		print "<ul><li><tt>\"".trim(preg_replace('/[^\w]+/',' ',$row['value']))."\"</tt></li></ul>";
	} else {
		print "<ul><li><tt>".he($row['value'])."</tt></li></ul>";
	}
	
	######
	# error reporting
	
	if ($row['profile'] == 'gurl') {

		if (preg_match('/topic=(\d+)/',$row['value'],$m) 
		|| preg_match('/\/gallery\/.*_(\d+)(\/\d+|)\s*$/',$row['value'],$m)) {

			//valid topic
			
		} elseif (preg_match('/article\/([\w-]+)$/',$row['value'],$m)) {
			//valid article

		} elseif (preg_match('/\/snippet\/(\d+)/',$row['value'],$m)) {
			//valid shared description
		} else {
			print "<div class=\"important\" style=\"margin-left:30px;background-color:pink\">Error: This does not appear to be a valid <b>URL</b> of a collection on Geograph Britain and Ireland - the rule will be ignored.</div>";
		}

	} elseif ($row['profile'] == 'words' || $row['profile'] == 'phrase') {
		if (preg_match('/[^\w ]+/',$row['value'])) {
			print "<div class=\"important\" style=\"margin-left:30px\">Warning: This rule contains non-word charactors. The above has been changed to reflect the words actully used</div>";
		}
	}
	if ($row['type'] == 'not' && $row['profile'] != 'ids') {
		if (isset($stats['any']['gurl']) || isset($stats['any']['ids'])) {
			print "<div class=\"important\" style=\"margin-left:30px\">Note: Remember this rule will not be applied to images added from a collection or directly via id(s).</div>";

		} elseif (empty($query['stats']['any']) && empty($query['stats']['all'])) {
			print "<div class=\"important\" style=\"margin-left:30px;background-color:pink\">Error: There is no word query for this to apply to - the rule will be ignored.</div>";
		}

	}

}
if ($last)
	print "</blockquote>";
#print_rp($query);
if (!empty($query['sphinx'])) {
	print "<br/><br/>";
	echo "<p style=\"background-color:silver;padding:5px;font-size:0.8em\" title=\"".he($query['sphinx'])."\">Can view results of the full text query created using the above filter rules, directly in the <a href=\"http://www.geograph.org.uk/search.php?searchtext=".urlencode($query['sphinx'])."&amp;do=1\">Geograph Search</a></p>";
}


include($_SERVER['DOCUMENT_ROOT'].'/portals/footer.inc.php');
